﻿' Copyright © Microsoft Corporation.  All Rights Reserved.
' This code released under the terms of the 
' Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
' <copyright file="ExpressionPlayerControl.vb" company="Microsoft">
'     Copyright © Microsoft Corporation. All rights reserved.
' </copyright>
' <author>Microsoft Expression Encoder Team</author>

Imports Microsoft.VisualBasic
Imports System
Imports System.Windows
Imports System.Windows.Browser
Imports System.Windows.Controls

Namespace ExpressionMediaPlayer
	''' <summary>
	''' Expression Media Player that supports adpative streaming
	''' </summary>
	Public Class ExpressionPlayer
		Inherits MediaPlayer
		''' <summary>
		''' template is showing preview content in preview of Encoder.
		''' </summary>
		Private m_InPreviewMode As Boolean
		''' <summary>
		''' Initializes a new instance of the ExpressionPlayer class.
		''' </summary>
		Public Sub New()
			MyBase.New()
		End Sub
		''' <summary>
		''' Performs additional initialization during the start up event.
		''' </summary>
		''' <param name="sender">The object triggering the startup event</param>
		''' <param name="e">The event arguments</param>
		Public Overrides Sub OnStartup(ByVal sender As Object, ByVal e As StartupEventArgs)
			MyBase.OnStartup(sender, e)

			If Playlist.Items.Count = 0 AndAlso Application.Current.InstallState=InstallState.NotInstalled Then
                Dim strFakeOutput As String = String.Empty
				Dim uriStockContent As Uri = HtmlPage.Document.DocumentUri
				HtmlPage.Document.QueryString.TryGetValue("fakeoutput", strFakeOutput)

				If strFakeOutput IsNot Nothing Then
					Try
						Dim urib As New UriBuilder(Uri.UnescapeDataString(strFakeOutput))
						uriStockContent = urib.Uri
					Catch e1 As UriFormatException
					End Try

					Playlist.Items.Clear()

					For items As Integer = 0 To 6
						Dim playlistItem As New PlaylistItem(Playlist.Items)
						playlistItem.MediaSource = New Uri(uriStockContent, "sl.wmv")
						playlistItem.IsAdaptiveStreaming = False
						playlistItem.ThumbSource = New Uri(uriStockContent, "sl1.jpg")
						playlistItem.Title = "Lorum Ipsum Dolar Sit"
						playlistItem.Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque auctor libero nec justo tempor bibendum."
						Playlist.Items.Add(playlistItem)
						For i As Integer = 1 To 3
							Dim chapter As New ChapterItem()
							chapter.Position = CDbl(i)
							chapter.ThumbSource = New Uri(uriStockContent, "sl" & i & ".jpg")
							chapter.Title = String.Empty
							playlistItem.Chapters.Add(chapter)
						Next i
					Next items

					m_InPreviewMode = True
				End If
			End If
		End Sub
		''' <summary>
		''' overrides GetTemplateChildren to disable some functionality when in preview mode
		''' </summary>
		Protected Overrides Sub GetTemplateChildren()
			MyBase.GetTemplateChildren()

			If m_InPreviewMode Then
				Dim offlineButton = TryCast(GetTemplateChild(MediaPlayer.OfflineButton), Button)
				If offlineButton IsNot Nothing Then
					offlineButton.IsEnabled = False
				End If

				Dim popoutButton = TryCast(GetTemplateChild(MediaPlayer.PopOutButton), Button)
				If popoutButton IsNot Nothing Then
					popoutButton.IsEnabled = False
				End If
			End If
		End Sub
	End Class
End Namespace
